package com.myhouse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.myhouse.biz.DistrictService;
import com.myhouse.biz.HouseService;
import com.myhouse.biz.StreetService;
import com.myhouse.biz.TypeService;
import com.myhouse.biz.UserService;
import com.myhouse.entity.District;
import com.myhouse.entity.House;
import com.myhouse.entity.Street;
import com.myhouse.entity.Type;
import com.myhouse.entity.User;

@Controller
public class AdminController {
	@Autowired
	private HouseService houseService;
	@Autowired
	private DistrictService districtService;
	@Autowired
	private StreetService streetService;
	@Autowired
	private TypeService typeService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/admin/list")
	public String list(Model model,String title,String sort,Integer houseSort){
		title=title==null?"0":title; //搜索内容
		sort=sort==null?"0":sort;     //类型
		
		houseSort=houseSort==null?2:houseSort; 
		
		if (sort.equals("1")) {	
				model.addAttribute("districts", districtService.getDistrictByTitle(title));			
				return "admin-list";
		}
		if (sort.equals("2")) {
			model.addAttribute("streets", streetService.getStreetByTitle(title));
			System.out.print(streetService.getStreetByTitle(title).size());
			System.out.print(title);
			return "admin-list";
			
		}
		
		if (sort.equals("3")) {
			model.addAttribute("types", typeService.getTypeByTitle(title));
			System.out.print(typeService.getTypeByTitle(title).size());
			System.out.print(title);
			return "admin-list";			
		}
		if (sort.equals("4")) {
			model.addAttribute("users", userService.getAllOrByTitle(title));
			return "admin-list";
		}
		model.addAttribute("districts", districtService.getAll());		
		model.addAttribute("streets", streetService.getAll());
		model.addAttribute("types", typeService.getAll());
		model.addAttribute("users", userService.getAllOrByTitle(null));
		//指定房屋状态传值
		List<House> list=houseService.getAllHouses();
		List<House> houses=new ArrayList<>();
		if (houseSort == 1) {
			for (House house : list) {
				if (house.getState() == 1) {
					houses.add(house);
				}
			}
			model.addAttribute("houses", houses);
		}
		if (houseSort == 0) {
			for (House house : list) {
				if (house.getState() == 0) {
					houses.add(house);
				}
			}
			model.addAttribute("houses", houses);
		}
		if (houseSort==2 || houseSort==null) {
			model.addAttribute("houses", houseService.getAllHouses());	
		}
			
		return "admin-list";
	}
	
	@RequestMapping("/admin/edit")
	public String edit(Model model,Integer sort,Integer id){
		id=id==null?0:id;
		if (id !=0) {
			if (sort == 1) {
				District district = districtService.getDistrictById(id);
				model.addAttribute("district", district);
			}
			if (sort == 2) {
				List<District> districts = districtService.getAll();
				model.addAttribute("districts", districts);
				model.addAttribute("street", streetService.getStreetById(id));
			}
			if (sort == 3) {
				List<Street> streets = streetService.getAll();
				model.addAttribute("type", typeService.getTypeByid(id));			
			}
		}else{
			if (sort == 2) {model.addAttribute("districts", districtService.getAll());}
		}
		return "admin-edit";
	}
	
	@RequestMapping("/admin/save1")
	public String Save (Model model,Integer sort,Integer id,@Valid District district){
		if(id>0){
			districtService.update(district);
		}else{
			districtService.add(district);
		}		
		return "redirect:list#quyu";
	}
	@RequestMapping("/admin/save2")
	public String Save (Model model,Integer sort,Integer id,@Valid Street street){
		if(id>0){
			streetService.update(street);
		}else{
			streetService.add(street);
		}		
		return "redirect:list#jiedao";
	}
	
	@RequestMapping("/admin/save3")
	public String Save (Model model,Integer sort,Integer id,@Valid Type type){
		if(id>0){
			typeService.update(type);
		}else{
			typeService.add(type);
		}		
		return "redirect:list#fangxing";
	}
	
	@RequestMapping("/admin/delete")
	public  String delete(Model model,Integer sort,Integer id ){
		if (sort==1) {districtService.delete(id);
		return "redirect:list#quyu";
		}
		if (sort==2) {streetService.delete(id);
		return "redirect:list#jiedao";
		}
		if (sort==3) {typeService.delete(id);
		return "redirect:list#fangxing";
		}
		if (sort==4) {
			houseService.deleteViolationHouses(id);
		return "redirect:list#fangxing";
		}
		return "redirect:list";	
		
	}
	@RequestMapping("/admin/update")
	public String update(Model model,Integer id,Integer isInUse){
		
		if (isInUse==0) {
			User user =userService.getUserById(id);
			user.setIsInUse(false);
			userService.updateUser(user);
		}
		if (isInUse==1) {
			User user =userService.getUserById(id);
			user.setIsInUse(true);
			userService.updateUser(user);
		}				
		return "redirect:list#yonghu";
	}
	
	

}
